﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using Castle.DynamicProxy;

namespace PlutoStudio.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //MefContainer.SetService<string>("str", "String");
            //Console.WriteLine(MefContainer.GetService<string>("str"));
            ////using (var context = new MefContext())
            ////{
            ////    MefContainer.SetService("str", "String2");
            ////    Console.WriteLine(MefContainer.GetService<string>("str"));
            ////}
            //Console.WriteLine(MefContainer.GetService<string>("str"));
            ////MefContainer.Register<Test, ITest>().SelectConstructor(b => new Test(b.Import<string>(c => c.AsContractName("str"))));
            //MefContainer.RegisterInterface(typeof(Test2));
            //MefContainer.Register<MyClass>();
            //Console.WriteLine(MefContainer.IsRegister<MyClass>());
            //var en = MefContainer.GetService<MyClass>();
            //en.TestM();
            //Console.WriteLine(MefContainer.GetService<MyClass>().Test.Name);
            var client = PlutoStudio.Net.Http.HttpClient.Create("http://www.baidu.com");
            Console.WriteLine(client.GetString());
            //var cl = new Test2();
            //Console.WriteLine(cl.ToXml());
        }
    }

    public interface ITest
    {
        string Name { get; }
    }

    public class Test : ITest
    {
        public Test()
        {

        }


        public Test(string str)
        {
            Name = str;
        }
        public string Name { get; set; }
    }
    [Log]
    public class MyClass
    {
        public MyClass()
        {

        }

        public MyClass(ITest test)
        {
            Test = test;
        }

        public virtual void TestM()
        {
            //Console.WriteLine("Test");
            //throw new Exception("测试异常");
        }

        public virtual ITest Test { get; set; }
    }
    public class Test2 : ITest
    {
        public string Name
        {
            get
            {
                return "Test2";
            }
        }
    }

    public class LogAttribute : Aop.InterceptorAttribute
    {
        public override void Intercept(IInvocation invocation)
        {
            try
            {
                Console.WriteLine(invocation.Method.Name);
                base.Intercept(invocation);

                var stackTrace = new System.Diagnostics.StackTrace(true);
                var stacks = stackTrace.GetFrames();
                foreach (var item in stacks.SkipWhile(i => i.GetMethod().Name != invocation.Method.Name))
                {
                    item.GetFileColumnNumber();
                    //Console.WriteLine(item.GetMethod());
                    //Console.WriteLine(item.GetMethod().ReflectedType.FullName);
                    Console.WriteLine(item);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                throw;
            }
        }
    }
}
